package com.wondersgroup.fjzlpt.console.dao.impl.role;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleDAO;

@Repository
public class RoleDAOImpl extends BaseJTDAOImpl implements RoleDAO{
	@Override
	@Autowired
	@Qualifier("TB_AUTH_ROLE")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	/**
	 * 查询角色概要列表
	 * @return
	 */
	public JSONArray querySummary(String roleScope){
		StringBuilder sbud = new StringBuilder("SELECT ROLEID, ROLENAME FROM ")
				.append(this.tb.getTableName()).append(" where super is null ");
		List list = new ArrayList();
		if(!StringUtils.isEmpty(roleScope)){
			sbud.append(" and ROLETYPE like ? ");
			list.add("%" + roleScope + "%");
		}
		SQLBean sb = new SQLBean(sbud.toString(), list.toArray(new String[list.size()]));
		return this.queryList(sb);
	}

	/**
	 * 查询角色概要列表
	 * @return
	 */
	public  JSONArray querySummaryAll(){
		StringBuilder sbud = new StringBuilder("SELECT ROLEID, ROLENAME FROM ")
				.append(this.tb.getTableName());
		SQLBean sb = new SQLBean(sbud.toString());
		return this.queryList(sb);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select * from ").append(this.tb.getTableName()).append(" where 1=1 \n");
		List list = new ArrayList();
		String value = map.get("ROLENAME");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and ROLENAME like ? ");
			list.add("%"+value+"%");
		}
		sbud.append(" order by roleid ");
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), " ", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
}
